package cn.hxlc.tie.demo.modular.work.controller;/*
 @author rui
 @date 2021/6/21 --18:53
*/


import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.modular.work.model.param.WorkShopParam;
import cn.hxlc.tie.demo.modular.work.service.WorkShopService;
import cn.hxlc.tie.libs.scanner.annotation.GetResource;
import cn.hxlc.tie.libs.scanner.annotation.PostResource;
import cn.hxlc.tie.libs.scanner.stereotype.ApiResource;
import cn.hxlc.tie.model.web.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Response;
import java.util.List;
import java.util.Map;

import static cn.hxlc.tie.model.web.response.ResponseData.DEFAULT_SUCCESS_CODE;

@RestController
@ApiResource(name = "车间管理", path = "/work")
@Api(tags = "车间")
public class WorkShopController {

    @Autowired
    private WorkShopService WorkShopService;

    @GetResource(name = "查询一个",path = "/findByOne",requiredLogin = false,requiredPermission = false)
    public  ResponseData findByOne(@RequestParam Long fid){

        return ResponseData.success( WorkShopService.findByOne(fid));
    }

    @PostResource(name = "删除",path = "/delete",requiredLogin = false,requiredPermission = false)
    public  ResponseData delete(@RequestBody WorkShopParam workShopParam){
        WorkShopService.delete(workShopParam);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"删除成功",null);
    }

    @PostResource(name = "新增", path = "/add",requiredPermission=false, requiredLogin = false)
    public  ResponseData add(@RequestBody WorkShopParam workShopParam) {

       WorkShopService.add(workShopParam);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"新增成功",null);

    }

    @GetResource(name = "分页查询", path = "/queryListPage", requiredPermission=false, requiredLogin = false)
    public ResponseData queryListPage(WorkShopParam workShopParam) {

        return ResponseData.success(WorkShopService.queryListPage(workShopParam));
    }

    @PostResource(name="更新",path = "/update",requiredPermission = false,requiredLogin = false)
    public ResponseData update(WorkShopParam workShopParam){
        WorkShopService.updateOne(workShopParam);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"修改成功",null);
    }

    @PostResource(name = "批量删除",path="/batchDelete",requiredPermission = false,requiredLogin = false)
    public ResponseData batchDelete(@RequestBody List<Integer> ids){
        WorkShopService.batchDelete(ids);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"删除成功",null);
    }
    @PostResource(name="批量启用，禁用",path = "/batchEdit",requiredLogin = false,requiredPermission = false)
    public ResponseData batchEdit(@RequestBody Map params){
        JSONArray jfids = (JSONArray)params.get("fid");
        Object []fid =jfids.toArray();
        Integer fstatus = (Integer)params.get("fstatus");
        WorkShopService.batch(fid, StatusEnum.toEnum(fstatus));
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"操作成功",null);
    }
}
